package com.itgm.jiayun_web_project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itgm.jiayun_web_project.pojo.PageResult;
import com.itgm.jiayun_web_project.pojo.Result;
import com.itgm.jiayun_web_project.pojo.User;
import com.itgm.jiayun_web_project.service.UserService;
import com.itgm.jiayun_web_project.service.impl.UserServiceImpl;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户管理接口")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    //老师分页查询接口
    @GetMapping("/page")
    public Result<PageResult<User>> page(
            @RequestParam Integer page,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam String type
    ) {
        log.info("分页查询");
        Page<User> userPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        if (name != null && !name.equals("")) {
            qw.like(User::getName, name);
        }
        qw.eq(User::getType,type);
        qw.orderByDesc(User::getUpdateTime);
        qw.orderByDesc(User::getName);
        Page<User> page1 = userService.page(userPage, qw);
        PageResult<User> pageResult = new PageResult<>();
        pageResult.setTotal(page1.getTotal());
        pageResult.setRecords(page1.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 新增老师 管理端
     * @param user
     * @return
     */
    @PostMapping("/teacher")
    public Result saveTeacher(@RequestBody User user){
        log.info("新增老师");
        user.setType("老师");
        user.setUpdateTime(LocalDateTime.now());
        return userService.save(user)?Result.success("新增成功"):Result.error("新增失败");
    }
}
